# ==============================================================================
# file name: SI-domain-descriptives.R
# authors: Bernhard Clemm / Sjifra de Leeuw
# date: February 18, 2022
# purpose: Plot domain ideology
# ==============================================================================

# SETUP ========================================================================

basedir <- paste0(dirname(dirname(
  rstudioapi::getSourceEditorContext()$path)), "/")
codedir <- paste0(basedir, "code/")
datadir <- paste0(basedir, "data/")

library(tidyverse)

domains <- read.csv(paste0(datadir, "traces/pl_us_traces_domains.csv"))

# Ranks ========================================================================

domains <- domains %>% 
  filter(score_domain != "") %>%
  group_by(country) %>%
  mutate(rank = 1:n())

domains %>% filter(country == "US") %>% arrange(desc(count)) %>% head(5)
domains %>% filter(country == "PL") %>% arrange(desc(count)) %>% head(5)

# Plot by ideology =============================================================

domains_plot_pl <- domains %>% 
  filter(!is.na(ideo) & country == "PL") %>%
  arrange(desc(ideo)) %>%
  mutate(order = 1:n(), x = 0,
         count_log = log(count),
         ideo_cat = factor(case_when(
           ideo <= -0.230076500 ~"Left", 
           ideo > -0.230076500 & ideo <= 0.220825000 ~"Center", 
           ideo > 0.220825000 ~ "Right"),
           ordered = TRUE, levels = c("Left", "Center", "Right"))) 

domains_plot_us <- domains %>% 
  filter(!is.na(ideo) & country == "US") %>%
  filter(count > 5) %>%
  arrange(desc(ideo)) %>%
  mutate(order = 1:n(), x = 0,
         count_log = log(count),
         ideo_cat = factor(case_when(
           ideo < -0.2 ~ "Left", 
           ideo >= -0.2 & ideo <= 0.09 ~ "Center",
           ideo > 0.09 ~ "Right"),
           ordered = TRUE, levels = c("Left", "Center", "Right"))) 

domain_plotting <- function(dat, fontsize) {
  plot <- dat %>% 
    ggplot() +
    geom_segment(aes(x = x, xend = ideo, y = order, yend = order), size = 0.5) + 
    geom_point(aes(x = ideo, y = order, size = count_log, fill = ideo), shape = 21) + 
    geom_text(data = subset(dat, ideo < 0), 
              aes(x = x, y = order, label = score_domain, hjust = 0), 
              vjust=0.5, size = fontsize, nudge_x = 0.05) +
    geom_text(data = subset(dat, ideo >= 0), 
              aes(x = x, y = order, label = score_domain, hjust=1), 
              vjust=0.5, size = fontsize, nudge_x = -0.05) +
    scale_fill_gradient(
      low = "white", high = "goldenrod1") + 
    facet_wrap(.~ ideo_cat, strip.position ="top", ncol = 3, scales = "free_y") +
    theme_minimal() + 
    theme(legend.position = 'none', 
          axis.title = element_blank(), 
          panel.grid = element_blank(), 
          axis.text = element_blank(),
          strip.text.x = element_blank())
  return(plot)
}

domain_plotting(domains_plot_pl, 3)
ggsave(paste0(figdir, "domains_pl_plot.png"), units = "cm", height = 11, width = 18)
domain_plotting(domains_plot_us, 1.5)
ggsave(paste0(figdir, "domains_us_plot.png"), units = "cm", height = 20, width = 18)

